package interficie;

/**
 * Created by Rafael Simon Garcia on 11/12/13.
 */

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Vector;

public class DisplayTaskElementsView extends JPanel {
    private static final long serialVersionUID = 1L;

    private JList list;
    private JTextArea subtaskNameValueTextArea, subTaskDescriptionValueTextArea, subTaskCostValueTextArea;
    private JButton saveButton, deleteSubTaskButton;
    private CtrlInterficie controller;
    private String oldSubTaskName;
    private DefaultListModel lm = new DefaultListModel();
    private String subtarea;
    private int indice;
    public DisplayTaskElementsView(CtrlInterficie c) {
        this.controller = c;
        this.InitializeWindow();
    }

    private void InitializeWindow(){
        setBackground(new Color(0, 153, 255));
        setLayout(null);
        setBounds(100, 100, 668, 531);

        add(TitleLabel());
        add(SubTasksScrollPane(SubTasksJList()));
        add(SubtareaNameLabel());
        add(SubTaskNameValueTextArea(""));
        add(DescriptionLabel());
        add(SubTaskDescriptionValueTextArea(""));
        add(TimeLabel());
        add(SubTaskCostValueTextArea(""));
        add(SavedSubTasksLabel());
        add(GoBackLabel());
        add(HomeButton());
        add(SaveButton());
        add(DeleteSubtaskButton());
    }

    private JButton SaveButton() {
        if(saveButton != null) return saveButton;

        saveButton = new JButton("Guardar Subtarea");
        saveButton.setFont(new Font("Tahoma", Font.BOLD, 13));
        saveButton.setBounds(410, 350, 200, 35);
        saveButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try{
                    int cost = Integer.parseInt(subTaskCostValueTextArea.getText());
                    if(cost < 0 || cost > 480) throw new NumberFormatException();

                    controller.modificarSubtarea(
                            oldSubTaskName, subtaskNameValueTextArea.getText(),
                            subTaskDescriptionValueTextArea.getText(), cost);
                    JOptionPane.showMessageDialog(null, "Subtarea: '" + subtaskNameValueTextArea.getText()  +
                            "' modificada exitosamente", "Guardando Tarea", JOptionPane.WARNING_MESSAGE);
                    

                }
                catch(NumberFormatException ex){
                    JOptionPane.showMessageDialog(null, "Debe Introducir valor entero positivo e "+
                            "igual o menor que 480 [minutos]", "Sólo Números", JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        saveButton.setEnabled(false);

        return saveButton;
    }

    private JButton DeleteSubtaskButton() {
        if(deleteSubTaskButton != null) return deleteSubTaskButton;

        deleteSubTaskButton = new JButton("Eliminar Subtarea");
        deleteSubTaskButton.setFont(new Font("Tahoma", Font.BOLD, 13));
        deleteSubTaskButton.setBounds(410, 395, 200, 35);
        deleteSubTaskButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //subtareas = controller.EliminarSubtarea(list.getSelectedValue());
                //AddSubTasksToListModel(subtareas);
            	System.out.println("VCoy a eliminar esta subtarea " + subtarea + " con este indice " + indice);
            	controller.eliminarInstanciasSubtarea(subtarea);
            	DefaultListModel m = (DefaultListModel)list.getModel();
            	vaciarCampos();
            	m.remove(indice);
            	list.setSelectedIndex(0);
            }
        });
        deleteSubTaskButton.setEnabled(false);

        return deleteSubTaskButton;
    }

    private JButton HomeButton() {
        JButton homeButton = new JButton("");
        homeButton.setOpaque(false);
        homeButton.setContentAreaFilled(false);
        homeButton.setBorderPainted(false);
        homeButton.setIcon(new ImageIcon(CreaTarea.class.getResource("/resources/home.png")));
        homeButton.setBounds(554, 33, 56, 61);
        homeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) { controller.Volver(); }
        });
        return homeButton;
    }

    private JLabel GoBackLabel() {
        JLabel lblVolver = new JLabel("Volver");
        lblVolver.setHorizontalAlignment(SwingConstants.CENTER);
        lblVolver.setFont(new Font("Tahoma", Font.BOLD, 15));
        lblVolver.setBounds(550, 95, 59, 14);
        return lblVolver;
    }

    private JLabel SavedSubTasksLabel() {
        JLabel lblNewLabel_1 = new JLabel("Subtareas Guardadas");
        lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel_1.setBounds(40, 125, 200, 20);
        return lblNewLabel_1;
    }

    private JLabel TimeLabel() {
        JLabel label = new JLabel("Costo en minutos:");
        label.setFont(new Font("Tahoma", Font.BOLD, 13));
        label.setBounds(350, 290, 200, 15);
        return label;
    }

    private JTextArea SubTaskCostValueTextArea(String cost) {
        if (subTaskCostValueTextArea != null) return subTaskCostValueTextArea;

        subTaskCostValueTextArea = new JTextArea(cost);

        SetInputPaneColors(subTaskCostValueTextArea);

        subTaskCostValueTextArea.setBounds(380, 310, 230, 25);
        subTaskCostValueTextArea.setOpaque(true);
        subTaskCostValueTextArea.setEditable(true);

        return subTaskCostValueTextArea;
    }

    private JLabel DescriptionLabel() {
        JLabel label = new JLabel("Descripción:");
        label.setFont(new Font("Tahoma", Font.BOLD, 13));
        label.setBounds(350, 190, 200, 15);
        return label;
    }

    private JTextArea SubTaskDescriptionValueTextArea(String description) {
        if (subTaskDescriptionValueTextArea != null) return subTaskDescriptionValueTextArea;

        subTaskDescriptionValueTextArea = new JTextArea(description);

        SetInputPaneColors(subTaskDescriptionValueTextArea);

        subTaskDescriptionValueTextArea.setBounds(380, 215, 230, 75);
        subTaskDescriptionValueTextArea.setOpaque(true);
        subTaskDescriptionValueTextArea.setEditable(true);

        return subTaskDescriptionValueTextArea;
    }

    private JTextArea SubTaskNameValueTextArea(String subtaskName){
        if(subtaskNameValueTextArea != null) return subtaskNameValueTextArea;

        subtaskNameValueTextArea = new JTextArea(subtaskName);
        SetInputPaneColors(subtaskNameValueTextArea);
        subtaskNameValueTextArea.setBounds(380, 150, 230, 25);
        subtaskNameValueTextArea.setOpaque(true);
        subtaskNameValueTextArea.setEditable(true);

        return subtaskNameValueTextArea;
    }

    private JLabel SubtareaNameLabel() {
        JLabel label = new JLabel("Nombre de la subtarea:");
        label.setFont(new Font("Tahoma", Font.BOLD, 13));
        label.setBounds(350, 125, 200, 20);
        return label;
    }

    private void SetInputPaneColors(JTextArea p){
        p.setBackground(new Color(51, 204, 255));
        p.setBorder(new LineBorder(new Color(102, 153, 255), 2, true));
    }

    private JList SubTasksJList() {
        if(list != null) return list;

        
        list = new JList(lm);
        list.setLayoutOrientation(JList.VERTICAL);

        list.setBorder(new LineBorder(new Color(102, 153, 255), 2, true));

        list.setFont(new Font("Tahoma", Font.BOLD, 13));

        list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);

        list.setBackground(new Color(51, 204, 255));
        list.setBounds(98, 369, 397, 205);

        list.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting()) return;
                if(list.getSelectedIndex() < 0) list.setSelectedIndex(0);
                subtarea = (String)list.getSelectedValue();
	                indice = list.getSelectedIndex();
	                System.out.println("actual indice? " + indice);
	                System.out.println("Subtarea seleccionada " + subtarea);
	                SetSubTaskFields((String)list.getSelectedValue());
                saveButton.setEnabled(true);
                deleteSubTaskButton.setEnabled(true);
            }
        });

        // fill the list with the subtasks
        AddSubTasksToListModel(controller.getSubtareas());

        return list;
    }

    private void AddSubTasksToListModel(ArrayList<String> tasks){
        DefaultListModel m = (DefaultListModel)list.getModel();
        m.clear();
        for(String name : tasks) m.addElement(name);
    }

    private JScrollPane SubTasksScrollPane(JList l) {
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(40, 154, 191, 270);
        scrollPane.setBackground(new Color(0, 153, 255));
        scrollPane.setViewportView(l);
        return scrollPane;
    }

    private JLabel TitleLabel() {
        JLabel label = new JLabel("Selecciona la subtarea que desees modificar");
        label.setFont(new Font("Tahoma", Font.BOLD, 15));
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setBounds(0, 38, 668, 34);
        return label;
    }

    private void SetSubTaskFields(String subTaskName){
        oldSubTaskName = subTaskName;
        Vector v = controller.getInfoSubtarea(subTaskName);
        subtaskNameValueTextArea.setText(v.get(0).toString());
        subTaskDescriptionValueTextArea.setText(v.get(1).toString());
        subTaskCostValueTextArea.setText(v.get(2).toString());
    }
    private void vaciarCampos(){
    	subtaskNameValueTextArea.setText("");
        subTaskDescriptionValueTextArea.setText("");
        subTaskCostValueTextArea.setText("");
    }
}